package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.RiskModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 投核保校验  HMT049
 * @author lyw
 * @date 2019-07-30 10:52:16
 */
public class Hmt049InsuranceStrategy implements InsuranceStrategy {

    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository,
                                                    ProductModel productModel, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        CustInfoModel custInfoModel = requestModel.getCustInfo();
        if (StringUtils.isEmpty(custInfoModel.getAppnt().getOccupationType())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0026_MESSAGE);
        }
        //被保人信息
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        PolicyInfoModel policyInfoModel = requestModel.getPolicyInfo();
        String prtNo = policyInfoModel.getPrtNo();
        String saleChnl = policyInfoModel.getSaleChnl();
        //被保人序号
        int prodInsSeq = productModel.getProdInsSeq();
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                String type = insurantModel.getOccupationType();
                //1.校验职业类别是否填写 d
                if (StringUtils.isEmpty(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0017_MESSAGE);
                }
                //3.非HCT011的险种，职业类别不能为5、6、7类
                if (productConfig.getInsurantTypeList().contains(type)) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NPGG0016_MESSAGE,type));
                }

                if (insurantModel.getBlackListFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE,insurantModel.getName()));
                }
                if (insurantModel.getClaimFlag() > 0) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0027,
                            String.format(UnderWriteConstant.UNDER_WRITE_NUGG0027_MESSAGE,insurantModel.getName()));
                }
            }
        }
        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                List<RiskModel> result = insuredCoreRepository.getMillionHealthInsuranceCount(prtNo, insurantModel.getInsuredNo());
                for (RiskModel riskModel : result) {
                    if (StringUtils.equals("1", riskModel.getFlag()) && StringUtils.equals("10", saleChnl)) {
                            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0032_MESSAGE);
                    }
                }
            }
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
